﻿using System;
using UnityEngine;

namespace UnityStandardAssets.CinematicEffects
{
	// Token: 0x02000467 RID: 1127
	[Serializable]
	public class SMAA : IAntiAliasing
	{
		// Token: 0x170001E6 RID: 486
		// (get) Token: 0x060017FA RID: 6138 RVA: 0x00065C61 File Offset: 0x00064061
		public Shader shader
		{
			get
			{
				if (this.m_Shader == null)
				{
					this.m_Shader = Shader.Find("Hidden/Subpixel Morphological Anti-aliasing");
				}
				return this.m_Shader;
			}
		}

		// Token: 0x170001E7 RID: 487
		// (get) Token: 0x060017FB RID: 6139 RVA: 0x00065C8A File Offset: 0x0006408A
		private Texture2D areaTexture
		{
			get
			{
				if (this.m_AreaTexture == null)
				{
					this.m_AreaTexture = Resources.Load<Texture2D>("AreaTex");
				}
				return this.m_AreaTexture;
			}
		}

		// Token: 0x170001E8 RID: 488
		// (get) Token: 0x060017FC RID: 6140 RVA: 0x00065CB3 File Offset: 0x000640B3
		private Texture2D searchTexture
		{
			get
			{
				if (this.m_SearchTexture == null)
				{
					this.m_SearchTexture = Resources.Load<Texture2D>("SearchTex");
				}
				return this.m_SearchTexture;
			}
		}

		// Token: 0x170001E9 RID: 489
		// (get) Token: 0x060017FD RID: 6141 RVA: 0x00065CDC File Offset: 0x000640DC
		private Material material
		{
			get
			{
				if (this.m_Material == null)
				{
					this.m_Material = ImageEffectHelper.CheckShaderAndCreateMaterial(this.shader);
				}
				return this.m_Material;
			}
		}

		// Token: 0x060017FE RID: 6142 RVA: 0x00065D06 File Offset: 0x00064106
		public void OnEnable(AntiAliasing owner)
		{
			if (!ImageEffectHelper.IsSupported(this.shader, true, false, owner))
			{
				owner.enabled = false;
			}
		}

		// Token: 0x060017FF RID: 6143 RVA: 0x00065D24 File Offset: 0x00064124
		public void OnDisable()
		{
			if (this.m_Material != null)
			{
				UnityEngine.Object.DestroyImmediate(this.m_Material);
			}
			if (this.m_Accumulation != null)
			{
				UnityEngine.Object.DestroyImmediate(this.m_Accumulation);
			}
			this.m_Material = null;
			this.m_Accumulation = null;
		}

		// Token: 0x06001800 RID: 6144 RVA: 0x00065D78 File Offset: 0x00064178
		public void OnPreCull(Camera camera)
		{
			if (this.temporal.UseTemporal())
			{
				this.m_ProjectionMatrix = camera.projectionMatrix;
				this.m_FlipFlop -= 2f * this.m_FlipFlop;
				Matrix4x4 identity = Matrix4x4.identity;
				identity.m03 = 0.25f * this.m_FlipFlop * this.temporal.fuzzSize / (float)camera.pixelWidth;
				identity.m13 = -0.25f * this.m_FlipFlop * this.temporal.fuzzSize / (float)camera.pixelHeight;
				camera.projectionMatrix = identity * camera.projectionMatrix;
			}
		}

		// Token: 0x06001801 RID: 6145 RVA: 0x00065E20 File Offset: 0x00064220
		public void OnPostRender(Camera camera)
		{
			if (this.temporal.UseTemporal())
			{
				camera.ResetProjectionMatrix();
			}
		}

		// Token: 0x06001802 RID: 6146 RVA: 0x00065E38 File Offset: 0x00064238
		public void OnRenderImage(Camera camera, RenderTexture source, RenderTexture destination)
		{
			int pixelWidth = camera.pixelWidth;
			int pixelHeight = camera.pixelHeight;
			bool flag = false;
			SMAA.QualitySettings qualitySettings = this.quality;
			if (this.settings.quality != SMAA.QualityPreset.Custom)
			{
				qualitySettings = SMAA.QualitySettings.presetQualitySettings[(int)this.settings.quality];
			}
			int edgeDetectionMethod = (int)this.settings.edgeDetectionMethod;
			int pass = 4;
			int pass2 = 5;
			int pass3 = 6;
			Matrix4x4 matrix4x = GL.GetGPUProjectionMatrix(this.m_ProjectionMatrix, true) * camera.worldToCameraMatrix;
			this.material.SetTexture("_AreaTex", this.areaTexture);
			this.material.SetTexture("_SearchTex", this.searchTexture);
			this.material.SetVector("_Metrics", new Vector4(1f / (float)pixelWidth, 1f / (float)pixelHeight, (float)pixelWidth, (float)pixelHeight));
			this.material.SetVector("_Params1", new Vector4(qualitySettings.threshold, qualitySettings.depthThreshold, (float)qualitySettings.maxSearchSteps, (float)qualitySettings.maxDiagonalSearchSteps));
			this.material.SetVector("_Params2", new Vector2((float)qualitySettings.cornerRounding, qualitySettings.localContrastAdaptationFactor));
			this.material.SetMatrix("_ReprojectionMatrix", this.m_PreviousViewProjectionMatrix * Matrix4x4.Inverse(matrix4x));
			float num = (this.m_FlipFlop >= 0f) ? 1f : 2f;
			this.material.SetVector("_SubsampleIndices", new Vector4(num, num, num, 0f));
			Shader.DisableKeyword("USE_PREDICATION");
			if (this.settings.edgeDetectionMethod == SMAA.EdgeDetectionMethod.Depth)
			{
				camera.depthTextureMode |= DepthTextureMode.Depth;
			}
			else if (this.predication.enabled)
			{
				camera.depthTextureMode |= DepthTextureMode.Depth;
				Shader.EnableKeyword("USE_PREDICATION");
				this.material.SetVector("_Params3", new Vector3(this.predication.threshold, this.predication.scale, this.predication.strength));
			}
			Shader.DisableKeyword("USE_DIAG_SEARCH");
			Shader.DisableKeyword("USE_CORNER_DETECTION");
			if (qualitySettings.diagonalDetection)
			{
				Shader.EnableKeyword("USE_DIAG_SEARCH");
			}
			if (qualitySettings.cornerDetection)
			{
				Shader.EnableKeyword("USE_CORNER_DETECTION");
			}
			Shader.DisableKeyword("USE_UV_BASED_REPROJECTION");
			if (this.temporal.UseTemporal())
			{
				Shader.EnableKeyword("USE_UV_BASED_REPROJECTION");
			}
			if (this.m_Accumulation == null || this.m_Accumulation.width != pixelWidth || this.m_Accumulation.height != pixelHeight)
			{
				if (this.m_Accumulation)
				{
					RenderTexture.ReleaseTemporary(this.m_Accumulation);
				}
				this.m_Accumulation = RenderTexture.GetTemporary(pixelWidth, pixelHeight, 0, source.format, RenderTextureReadWrite.Linear);
				this.m_Accumulation.hideFlags = HideFlags.HideAndDontSave;
				flag = true;
			}
			RenderTexture renderTexture = this.TempRT(pixelWidth, pixelHeight, source.format);
			Graphics.Blit(null, renderTexture, this.material, 0);
			Graphics.Blit(source, renderTexture, this.material, edgeDetectionMethod);
			if (this.settings.debugPass == SMAA.DebugPass.Edges)
			{
				Graphics.Blit(renderTexture, destination);
			}
			else
			{
				RenderTexture renderTexture2 = this.TempRT(pixelWidth, pixelHeight, source.format);
				Graphics.Blit(null, renderTexture2, this.material, 0);
				Graphics.Blit(renderTexture, renderTexture2, this.material, pass);
				if (this.settings.debugPass == SMAA.DebugPass.Weights)
				{
					Graphics.Blit(renderTexture2, destination);
				}
				else
				{
					this.material.SetTexture("_BlendTex", renderTexture2);
					if (this.temporal.UseTemporal())
					{
						Graphics.Blit(source, renderTexture, this.material, pass2);
						if (this.settings.debugPass == SMAA.DebugPass.Accumulation)
						{
							Graphics.Blit(this.m_Accumulation, destination);
						}
						else if (!flag)
						{
							this.material.SetTexture("_AccumulationTex", this.m_Accumulation);
							Graphics.Blit(renderTexture, destination, this.material, pass3);
						}
						else
						{
							Graphics.Blit(renderTexture, destination);
						}
						Graphics.Blit(destination, this.m_Accumulation);
						RenderTexture.active = null;
					}
					else
					{
						Graphics.Blit(source, destination, this.material, pass2);
					}
				}
				RenderTexture.ReleaseTemporary(renderTexture2);
			}
			RenderTexture.ReleaseTemporary(renderTexture);
			this.m_PreviousViewProjectionMatrix = matrix4x;
		}

		// Token: 0x06001803 RID: 6147 RVA: 0x00066298 File Offset: 0x00064698
		private RenderTexture TempRT(int width, int height, RenderTextureFormat format)
		{
			int depthBuffer = 0;
			return RenderTexture.GetTemporary(width, height, depthBuffer, format, RenderTextureReadWrite.Linear);
		}

		// Token: 0x04000D34 RID: 3380
		[SMAA.TopLevelSettings]
		public SMAA.GlobalSettings settings = SMAA.GlobalSettings.defaultSettings;

		// Token: 0x04000D35 RID: 3381
		[SMAA.SettingsGroup]
		public SMAA.QualitySettings quality = SMAA.QualitySettings.presetQualitySettings[2];

		// Token: 0x04000D36 RID: 3382
		[SMAA.SettingsGroup]
		public SMAA.PredicationSettings predication = SMAA.PredicationSettings.defaultSettings;

		// Token: 0x04000D37 RID: 3383
		[SMAA.SettingsGroup]
		[SMAA.ExperimentalGroup]
		public SMAA.TemporalSettings temporal = SMAA.TemporalSettings.defaultSettings;

		// Token: 0x04000D38 RID: 3384
		private Matrix4x4 m_ProjectionMatrix;

		// Token: 0x04000D39 RID: 3385
		private Matrix4x4 m_PreviousViewProjectionMatrix;

		// Token: 0x04000D3A RID: 3386
		private float m_FlipFlop = 1f;

		// Token: 0x04000D3B RID: 3387
		private RenderTexture m_Accumulation;

		// Token: 0x04000D3C RID: 3388
		private Shader m_Shader;

		// Token: 0x04000D3D RID: 3389
		private Texture2D m_AreaTexture;

		// Token: 0x04000D3E RID: 3390
		private Texture2D m_SearchTexture;

		// Token: 0x04000D3F RID: 3391
		private Material m_Material;

		// Token: 0x02000468 RID: 1128
		[AttributeUsage(AttributeTargets.Field)]
		public class SettingsGroup : Attribute
		{
		}

		// Token: 0x02000469 RID: 1129
		[AttributeUsage(AttributeTargets.Field)]
		public class TopLevelSettings : Attribute
		{
		}

		// Token: 0x0200046A RID: 1130
		[AttributeUsage(AttributeTargets.Field)]
		public class ExperimentalGroup : Attribute
		{
		}

		// Token: 0x0200046B RID: 1131
		public enum DebugPass
		{
			// Token: 0x04000D41 RID: 3393
			Off,
			// Token: 0x04000D42 RID: 3394
			Edges,
			// Token: 0x04000D43 RID: 3395
			Weights,
			// Token: 0x04000D44 RID: 3396
			Accumulation
		}

		// Token: 0x0200046C RID: 1132
		public enum QualityPreset
		{
			// Token: 0x04000D46 RID: 3398
			Low,
			// Token: 0x04000D47 RID: 3399
			Medium,
			// Token: 0x04000D48 RID: 3400
			High,
			// Token: 0x04000D49 RID: 3401
			Ultra,
			// Token: 0x04000D4A RID: 3402
			Custom
		}

		// Token: 0x0200046D RID: 1133
		public enum EdgeDetectionMethod
		{
			// Token: 0x04000D4C RID: 3404
			Luma = 1,
			// Token: 0x04000D4D RID: 3405
			Color,
			// Token: 0x04000D4E RID: 3406
			Depth
		}

		// Token: 0x0200046E RID: 1134
		[Serializable]
		public struct GlobalSettings
		{
			// Token: 0x170001EA RID: 490
			// (get) Token: 0x06001807 RID: 6151 RVA: 0x000662CC File Offset: 0x000646CC
			public static SMAA.GlobalSettings defaultSettings
			{
				get
				{
					return new SMAA.GlobalSettings
					{
						debugPass = SMAA.DebugPass.Off,
						quality = SMAA.QualityPreset.High,
						edgeDetectionMethod = SMAA.EdgeDetectionMethod.Color
					};
				}
			}

			// Token: 0x04000D4F RID: 3407
			[Tooltip("Use this to fine tune your settings when working in Custom quality mode. \"Accumulation\" only works when \"Temporal Filtering\" is enabled.")]
			public SMAA.DebugPass debugPass;

			// Token: 0x04000D50 RID: 3408
			[Tooltip("Low: 60% of the quality.\nMedium: 80% of the quality.\nHigh: 95% of the quality.\nUltra: 99% of the quality (overkill).")]
			public SMAA.QualityPreset quality;

			// Token: 0x04000D51 RID: 3409
			[Tooltip("You've three edge detection methods to choose from: luma, color or depth.\nThey represent different quality/performance and anti-aliasing/sharpness tradeoffs, so our recommendation is for you to choose the one that best suits your particular scenario:\n\n- Depth edge detection is usually the fastest but it may miss some edges.\n- Luma edge detection is usually more expensive than depth edge detection, but catches visible edges that depth edge detection can miss.\n- Color edge detection is usually the most expensive one but catches chroma-only edges.")]
			public SMAA.EdgeDetectionMethod edgeDetectionMethod;
		}

		// Token: 0x0200046F RID: 1135
		[Serializable]
		public struct QualitySettings
		{
			// Token: 0x04000D52 RID: 3410
			[Tooltip("Enables/Disables diagonal processing.")]
			public bool diagonalDetection;

			// Token: 0x04000D53 RID: 3411
			[Tooltip("Enables/Disables corner detection. Leave this on to avoid blurry corners.")]
			public bool cornerDetection;

			// Token: 0x04000D54 RID: 3412
			[Range(0f, 0.5f)]
			[Tooltip("Specifies the threshold or sensitivity to edges. Lowering this value you will be able to detect more edges at the expense of performance.\n0.1 is a reasonable value, and allows to catch most visible edges. 0.05 is a rather overkill value, that allows to catch 'em all.")]
			public float threshold;

			// Token: 0x04000D55 RID: 3413
			[Min(0.0001f)]
			[Tooltip("Specifies the threshold for depth edge detection. Lowering this value you will be able to detect more edges at the expense of performance.")]
			public float depthThreshold;

			// Token: 0x04000D56 RID: 3414
			[Range(0f, 112f)]
			[Tooltip("Specifies the maximum steps performed in the horizontal/vertical pattern searches, at each side of the pixel.\nIn number of pixels, it's actually the double. So the maximum line length perfectly handled by, for example 16, is 64 (by perfectly, we meant that longer lines won't look as good, but still antialiased).")]
			public int maxSearchSteps;

			// Token: 0x04000D57 RID: 3415
			[Range(0f, 20f)]
			[Tooltip("Specifies the maximum steps performed in the diagonal pattern searches, at each side of the pixel. In this case we jump one pixel at time, instead of two.\nOn high-end machines it is cheap (between a 0.8x and 0.9x slower for 16 steps), but it can have a significant impact on older machines.")]
			public int maxDiagonalSearchSteps;

			// Token: 0x04000D58 RID: 3416
			[Range(0f, 100f)]
			[Tooltip("Specifies how much sharp corners will be rounded.")]
			public int cornerRounding;

			// Token: 0x04000D59 RID: 3417
			[Min(0f)]
			[Tooltip("If there is an neighbor edge that has a local contrast factor times bigger contrast than current edge, current edge will be discarded.\nThis allows to eliminate spurious crossing edges, and is based on the fact that, if there is too much contrast in a direction, that will hide perceptually contrast in the other neighbors.")]
			public float localContrastAdaptationFactor;

			// Token: 0x04000D5A RID: 3418
			public static SMAA.QualitySettings[] presetQualitySettings = new SMAA.QualitySettings[]
			{
				new SMAA.QualitySettings
				{
					diagonalDetection = false,
					cornerDetection = false,
					threshold = 0.15f,
					depthThreshold = 0.01f,
					maxSearchSteps = 4,
					maxDiagonalSearchSteps = 8,
					cornerRounding = 25,
					localContrastAdaptationFactor = 2f
				},
				new SMAA.QualitySettings
				{
					diagonalDetection = false,
					cornerDetection = false,
					threshold = 0.1f,
					depthThreshold = 0.01f,
					maxSearchSteps = 8,
					maxDiagonalSearchSteps = 8,
					cornerRounding = 25,
					localContrastAdaptationFactor = 2f
				},
				new SMAA.QualitySettings
				{
					diagonalDetection = true,
					cornerDetection = true,
					threshold = 0.1f,
					depthThreshold = 0.01f,
					maxSearchSteps = 16,
					maxDiagonalSearchSteps = 8,
					cornerRounding = 25,
					localContrastAdaptationFactor = 2f
				},
				new SMAA.QualitySettings
				{
					diagonalDetection = true,
					cornerDetection = true,
					threshold = 0.05f,
					depthThreshold = 0.01f,
					maxSearchSteps = 32,
					maxDiagonalSearchSteps = 16,
					cornerRounding = 25,
					localContrastAdaptationFactor = 2f
				}
			};
		}

		// Token: 0x02000470 RID: 1136
		[Serializable]
		public struct TemporalSettings
		{
			// Token: 0x06001809 RID: 6153 RVA: 0x0006649F File Offset: 0x0006489F
			public bool UseTemporal()
			{
				return this.enabled;
			}

			// Token: 0x170001EB RID: 491
			// (get) Token: 0x0600180A RID: 6154 RVA: 0x000664A8 File Offset: 0x000648A8
			public static SMAA.TemporalSettings defaultSettings
			{
				get
				{
					return new SMAA.TemporalSettings
					{
						enabled = false,
						fuzzSize = 2f
					};
				}
			}

			// Token: 0x04000D5B RID: 3419
			[Tooltip("Temporal filtering makes it possible for the SMAA algorithm to benefit from minute subpixel information available that has been accumulated over many frames.")]
			public bool enabled;

			// Token: 0x04000D5C RID: 3420
			[Range(0.5f, 10f)]
			[Tooltip("The size of the fuzz-displacement (jitter) in pixels applied to the camera's perspective projection matrix.\nUsed for 2x temporal anti-aliasing.")]
			public float fuzzSize;
		}

		// Token: 0x02000471 RID: 1137
		[Serializable]
		public struct PredicationSettings
		{
			// Token: 0x170001EC RID: 492
			// (get) Token: 0x0600180B RID: 6155 RVA: 0x000664D4 File Offset: 0x000648D4
			public static SMAA.PredicationSettings defaultSettings
			{
				get
				{
					return new SMAA.PredicationSettings
					{
						enabled = false,
						threshold = 0.01f,
						scale = 2f,
						strength = 0.4f
					};
				}
			}

			// Token: 0x04000D5D RID: 3421
			[Tooltip("Predicated thresholding allows to better preserve texture details and to improve performance, by decreasing the number of detected edges using an additional buffer (the detph buffer).\nIt locally decreases the luma or color threshold if an edge is found in an additional buffer (so the global threshold can be higher).")]
			public bool enabled;

			// Token: 0x04000D5E RID: 3422
			[Min(0.0001f)]
			[Tooltip("Threshold to be used in the additional predication buffer.")]
			public float threshold;

			// Token: 0x04000D5F RID: 3423
			[Range(1f, 5f)]
			[Tooltip("How much to scale the global threshold used for luma or color edge detection when using predication.")]
			public float scale;

			// Token: 0x04000D60 RID: 3424
			[Range(0f, 1f)]
			[Tooltip("How much to locally decrease the threshold.")]
			public float strength;
		}
	}
}
